//
//  NSString+Extension.m
//  华仔微博2期
//
//  Created by tarena on 15/8/13.
//  Copyright (c) 2015年 tarena. All rights reserved.
//

#import "NSString+Extension.h"
#import "NSDate+Extension.h"

@implementation NSString (Extension)

- (CGSize)sizeWithfont:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}


- (CGSize)sizeWithfont:(UIFont *)font
{
    return [self sizeWithfont:font maxW:MAXFLOAT];
}


#pragma mark - 时间格式化
-(NSString *)getResultTime:(NSString *)time isAccountListTime:(BOOL)accountListTime isDay:(BOOL)day{
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.locale= [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [fmt setTimeZone:timeZone];
    fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    //预约创建时间
    NSRange rang = {0,19};
    NSString *dateTime;
    if (![time isEqualToString:@""]) {
        
        dateTime = [time substringWithRange:rang];
        
    }
    
    NSDateFormatter *ff = [[NSDateFormatter alloc]init];
    ff.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //把字符串转换成日期对象
    NSDate *createDate = [ff dateFromString:dateTime];
    
    NSString *createDateStr = nil;
    
    if (!accountListTime) {
        
        if ([createDate isThisYear]){//今年
            if ([createDate isThisYesterday]){//昨天
                fmt.dateFormat = @"昨天 HH:mm";
                createDateStr  =  [fmt stringFromDate:createDate];
                
            }else if ([createDate isThisToday]){//今天
                fmt.dateFormat = @"HH:mm";
                createDateStr  =  [fmt stringFromDate:createDate];
                
            }else{//今天的其他日子
                fmt.dateFormat = @"MM-dd HH:mm";
                createDateStr  =  [fmt stringFromDate:createDate];
                
            }
        }else{//非今年
            fmt.dateFormat = @"MM-dd HH:mm";
            createDateStr  =  [fmt stringFromDate:createDate];
        }

    }else{
    
        if (day) {//是日期
            
            if ([createDate isThisYear]){//今年
                if ([createDate isThisYesterday]){//昨天
                    fmt.dateFormat = @"昨天";
                    createDateStr  =  [fmt stringFromDate:createDate];
                }else if ([createDate isThisToday]){//今天
                    fmt.dateFormat = @"今天";
                    createDateStr  =  [fmt stringFromDate:createDate];
                }else{//今天的其他日子
                    fmt.dateFormat = @"MM-dd";
                    createDateStr  =  [fmt stringFromDate:createDate];
                }
            }else{//非今年
                fmt.dateFormat = @"MM-dd";
                createDateStr  =  [fmt stringFromDate:createDate];
            }
        }else{
            
            fmt.dateFormat = @"HH:mm";
            createDateStr  =  [fmt stringFromDate:createDate];
            
        }

    
    }
    
    
    return createDateStr;
    
}

@end
